<template>
  <div class="container">
    <van-nav-bar
        title="标题"
        left-text="返回"
        left-arrow
        @click-left="onClickLeft"
        @click-right="onClickRight"
    >
      <template #title>
        <div class="search">
          <div class="icon">
            <van-icon name="search" color="#0e35b9" size="20"/>
          </div>
          <div class="enterInformation">
            <input
                type="text"
                placeholder="输入商品名称"
                v-model="searchValue"
                @change="handleChange(searchValue)"
            />
          </div>
        </div>
      </template>
      <template #right>
        <span>取消</span>
      </template>
    </van-nav-bar>
    <!-- 搜索内容 -->
    <div class="main">
      <div class="blue"></div>
      <div class="result" v-if="resultArray.length > 0">
        <div
            class="result_item"
            v-for="(item, index) in resultArray"
            :key="index"
            @click="toProductDetails(item)"
        >
          <div class="item_img">
            <img class="auto-img" :src="item.smallImg"/>
          </div>
          <div class="item_text">
            <div class="cName">
              <span>{{ item.name }}</span>
            </div>
            <div class="eName">
              <span>{{ item.enname | enName }}</span>
            </div>
            <div class="price">
              <span>￥{{ item.price }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- 搜索结果为空 -->
    <!-- 搜索提示 -->
    <van-empty
        v-if="searchType == true"
        image="search"
        description="请重新输入关键字"
    />
  </div>
</template>
<script>
import {search} from "@/api/search";

export default {
  data() {
    return {
      searchValue: "",
      resultArray: [],
      searchType: false,
    };
  },

  methods: {
    //  返回按钮
    onClickLeft() {
      this.$router.back();
    },
    // 取消按钮
    onClickRight() {
    },
    // 回车进行搜索
    handleChange(val) {
      let self = this;
      search({
        name: val,
      }).then((res) => {
        console.log(res);
        if (res.data.result.length == 0) {
          self.searchType = true;
          self.searchValue = "";
        } else {
          self.searchType = false;
        }
        self.resultArray = res.data.result;
      });
    },
    // 前往商品详情
    toProductDetails(item) {
      this.$router.push({name: "productDetails", query: {pid: item.pid}});
    },
  },
};
</script>
<style scoped lang="less">
.container {
  background: #f7f7f7;
  width: 100%;
  height: 100%;

  .search {
    box-sizing: border-box;
    background: #f7f8fa;
    width: 100%;
    height: 40px;
    line-height: 40px;
    justify-content: space-around;
    border-radius: 20px;
    align-items: center;
    display: flex;

    .icon {
      flex: 1;
      text-align: center;
      margin-left: 5px;
    }

    .enterInformation {
      flex: 9;

      input {
        width: 90%;
        background: #f7f7f7;
        font-size: 14px;
      }
    }
  }

  .main {
    width: 100%;
    margin: 0;
    padding: 0;

    .blue {
      width: 100%;
      height: 100px;
      background: #0c34ba;
    }

    .result {
      background: #ffffff;
      margin: -20px 10px 0 10px;
      border-radius: 5px;
      border: 1px solid #ffffff;
      display: flex;
      flex-wrap: wrap;
      justify-content: space-around;
      padding: 5px;

      .result_item {
        width: 30%;
        margin: 3px;

        .item_text {
          div {
            margin: 3px 0;
          }

          .cName {
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
          }

          .eName {
            color: #999;
            font-size: 12px;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
          }

          .price {
            font-weight: bold;
            color: #0c34ba;
            font-size: 18px;
          }
        }
      }
    }
  }
}
</style>
